// Initialize your app
function orderCtrl($scope) {
    $scope.name = '呵呵';
}
var myApp = new Framework7({
        cache:false,
        pushState:true,
        swipePanel:'left',
        swipePanelThreshold:20,
        swipeBackPageAnimateShadow:false,//页面返回特效
        swipeBackPageAnimateOpacity:false,//页面返回特效
        swipeBackPageActiveArea:500,
        template7Pages: true,
        template7Data: {
            'page:order':{
                //'name':'<a style="display: none">lihanbn</a>'
            }
        }

    });

// Export selectors engine
var $$ = Dom7;
myApp.hideToolbar(toolbar)
// Add view
var mainView = myApp.addView('#loginView', {
    // Because we use fixed-through navbar we can enable dynamic navbar
    dynamicNavbar: true,
    cache:false,
    pushState:true
});
//var mainView = myApp.addView('#orderView', {
//    // Because we use fixed-through navbar we can enable dynamic navbar
//    dynamicNavbar: true,
//    cache:false
//});
var messageView = myApp.addView('#messageView', {
    // Because we use fixed-through navbar we can enable dynamic navbar
    dynamicNavbar: true,
    cache:false,
    pushState:true
});
var messageView = myApp.addView('#serviceView', {
    // Because we use fixed-through navbar we can enable dynamic navbar
    dynamicNavbar: true,
    cache:false,
    pushState:true
});
//var messageView = myApp.addView('#messageView', {
//    // Because we use fixed-through navbar we can enable dynamic navbar
//    dynamicNavbar: true,
//    cache:false
//});
//console.log(myApp);
//console.log();
//myApp.showTab("#messageView");
myApp.onPageInit('login', function (page) {
    // run createContentPage func after link was clicked
    alert(1)
});
$('#searchMessage').focus(function () {
    $('.searchbar-overlay').addClass('searchbar-overlay-active');
    //alert($('.searchbar-overlay'));
})
$('#searchMessage').blur(function () {
    $('.searchbar-overlay').removeClass('searchbar-overlay-active');
    $('.searchbar').removeClass('searchbar-active');
    //alert($('.searchbar-overlay'));
})
$('.searchbar-overlay').on('click', function () {
    $(this).removeClass('searchbar-overlay-active');
    //$('.searchbar-cancel').css({'margin-right':'-51px!important'})

})
$('#messageView').on('show', function () {
    var mySearchbar = myApp.searchbar('.searchbar', {
        searchList: '.list-block-search',
        searchIn: '.item-title',
        overlay: '.searchbar-overlay'
    });
})
myApp.onPageInit('order_info', function (page) {
    // run createContentPage func after link was clicked
});

$('#loginView').on('show',function(){
    myApp.template7Data['page:order']['name'] = '000';
    //mainView.router.refreshPage()
    var newPageContent = '<div class="page" data-page="my-page">' +
        '<div class="page-content">' +
        '<p>Here comes new page</p><p>Here comes new page</p><p>Here comes new page</p><p>Here comes new page</p>' +
        '</div>' +
        '</div>';

//Load new content as new page
    mainView.router.loadContent(newPageContent);
})

$$(document).on('pageBeforeInit', function (e) {
    myApp.closePanel();
    var page = e.detail.page;
    switch (page.name){
        case 'message_content':
            //messageView.hideToolbar('.toolbar.my_toolbar');
            messageInit()
            break;
        case 'order':
            //
            myApp.template7Data['page:order']['name'] = '000';
            //console.log(myApp.template7Data['page:order']['name']);

            break;
    }



});



$$(document).on('pageAfterAnimation', function (e) {
    var page = e.detail.page;
    //console.log(page);
});
//$$(document).on('pageAfterAnimation', function (e) {
//    var page = e.detail.page;
//    console.log(page);
//});

// Callbacks to run specific code for specific pages, for example for About page:
myApp.onPageInit('message1', function (page) {
    // run createContentPage func after link was clicked
    $$('.create-page').on('click', function () {
        createContentPage();
    });
});

// Generate dynamic page
var dynamicPageIndex = 0;
function createContentPage() {
	mainView.router.loadContent(
        '<!-- Top Navbar-->' +
        '<div class="navbar">' +
        '  <div class="navbar-inner">' +
        '    <div class="left"><a href="#" class="back link"><i class="icon icon-back"></i><span>Back</span></a></div>' +
        '    <div class="center sliding">Dynamic Page ' + (++dynamicPageIndex) + '</div>' +
        '  </div>' +
        '</div>' +
        '<div class="pages">' +
        '  <!-- Page, data-page contains page name-->' +
        '  <div data-page="dynamic-pages" class="page">' +
        '    <!-- Scrollable page content-->' +
        '    <div class="page-content">' +
        '      <div class="content-block">' +
        '        <div class="content-block-inner">' +
        '          <p>Here is a dynamic page created on ' + new Date() + ' !</p>' +
        '          <p>Go <a href="#" class="back">back</a> or go to <a href="services.html">Services</a>.</p>' +
        '        </div>' +
        '      </div>' +
        '    </div>' +
        '  </div>' +
        '</div>'
    );
	return;
}

function messageInit(){
    var conversationStarted = false;

// Init Messages
    var myMessages = myApp.messages('.messages', {
        autoLayout:true
    });

// Init Messagebar
    var myMessagebar = myApp.messagebar('.messagebar');

// Handle message
    $$('.messagebar .link').on('click', function () {
        // Message text

        var messageText = myMessagebar.value().trim();
        // Exit if empy message
        if (messageText.length === 0) return;

        // Empty messagebar
        myMessagebar.clear()

        // 随机消息类型
        var messageType = (['sent', 'received'])[Math.round(Math.random())];

        // 接收的消息的头像和名称
        var avatar, name;
        if(messageType === 'received') {
            avatar = 'http://lorempixel.com/output/people-q-c-100-100-9.jpg';
            name = 'Kate';
        }
        // Add message
        myMessages.addMessage({
            // Message text
            text: messageText,
            // 随机消息类型
            type: messageType,
            // 头像和名称
            avatar: avatar,
            name: name,
            // 日期
            day: !conversationStarted ? 'Today' : false,
            time: !conversationStarted ? (new Date()).getHours() + ':' + (new Date()).getMinutes() : false
        })

        // 更新会话flag
        conversationStarted = true;
    });
}

